package com.example.lenovo.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.lenovo.demo.view.customview.TimerUtil;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;

public class RegisterActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private Button btn_verify;
    private EditText et_number;
    private EditText et_verify;
    private Button btn_next;

    //private  int i ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        toolbar = (Toolbar) findViewById(R.id.register_toolbar);
        toolbar.setTitle("注册携程账号");
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.mipmap.register_left);
        // getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        btn_verify = (Button) findViewById(R.id.btn_verify);
        et_number = (EditText) findViewById(R.id.et_number);
        et_verify = (EditText) findViewById(R.id.et_password);

        btn_next = (Button) findViewById(R.id.btn_next);

        SMSSDK.initSDK(this, "1a84e8f5d9ba6", "b26d93d1d3c1180df5c88e08317a6e03");

        //打开注册页面
        RegisterPage registerPage = new RegisterPage();
//        registerPage.setRegisterCallback(new EventHandler() {
//            public void afterEvent(int event, int result, Object data) {
//// 解析注册结果
//                if (result == SMSSDK.RESULT_COMPLETE) {
//                    @SuppressWarnings("unchecked")
//                    HashMap<String,Object> phoneMap = (HashMap<String, Object>) data;
//                    String country = (String) phoneMap.get("country");
//                    String phone = (String) phoneMap.get("phone");
//
//// 提交用户信息（此方法可以不调用）
//                    //   registerUser(country, phone);
//                }
//            }
//        });
//        registerPage.show(this);

        EventHandler eh = new EventHandler(){
            @Override
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE) {
                    //回调完成
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        if (result==SMSSDK.RESULT_COMPLETE){
                        //  btn_verify.equals(result);
//                          btn_next.setOnClickListener(new View.OnClickListener() {
//                              @Override
//                              public void onClick(View view) {
//                                  Toast.makeText(RegisterActivity.this, "陈宫", Toast.LENGTH_SHORT).show();
//                              }
//                          });
                        }
                        //提交验证码成功
                    }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
                        //获取验证码成功
                    }else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
                        //返回支持发送验证码的国家列表
                    }
                }else{
                    ((Throwable)data).printStackTrace();
                }
            }
        };

        SMSSDK.registerEventHandler(eh); //注册短信回调


        btn_verify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String number = et_number.getText().toString().trim();
                if(TextUtils.isEmpty(number)){
                    //   ToastUtil.showShort(MainActivity.this,"请输入电话号码");
                    Toast.makeText(RegisterActivity.this, "请输入电话号码", Toast.LENGTH_SHORT).show();
                }else {
                    if(number.length()!=11){
                        //    ToastUtil.showShort(MainActivity.this,"请输入正确的手机号格式");
                        Toast.makeText(RegisterActivity.this, "请输入正确的手机号格式", Toast.LENGTH_SHORT).show();
                    }else {
                        new TimerUtil(btn_verify,1000*60,1000).start();
                        SMSSDK.getVerificationCode("86",number);
                    }
                }
            }
        });

    }

//    public void click(View v){
//        switch (v.getId()) {
//            case R.id.btn_next:
//                Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
//                break;
//        }
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解除注册
        SMSSDK.unregisterAllEventHandler();
    }



}
